package com.loong.game2048.controller.handle;

import com.loong.game2048.Exception.BizException;
import com.loong.game2048.module.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 异常处理类
 * @author lyx
 * @date 2020/5/12 10:43
 */
@Aspect
@Component
@Slf4j
public class ExceptionHandleController {

    @Around(value = "execution(public com.loong.game2048.module.vo.Result com.loong.game2048.controller..*(..))")
    public Object handlerControllerMethod(ProceedingJoinPoint pjp) {

        Result<?> result;

        try {
            result = (Result<?>) pjp.proceed();
        } catch (Throwable e) {
            result = handlerException(pjp, e);
        }
        return result;
    }

    private Result<?> handlerException(ProceedingJoinPoint pjp, Throwable e) {
        Result<?> result;

        // 已知异常
        if (e instanceof BizException) {

            result = new Result<>((BizException) e);
        } else {
            // 发生未知异常
            result = new Result<>(e);
            e.printStackTrace();
            //发送异常邮箱通知
            //TODO 邮箱通知已关闭
            //InformationUtil.sendFreemarker(e);
//            log.info("邮箱通知已发送");
        }
        return result;
    }

}
